package com.tanhua.app.controller;

import com.aliyun.oss.ClientException;
import com.tanhua.app.service.UserService;
import com.tanhua.db.User;
import com.tanhua.db.UserInfo;
import com.tanhua.vo.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UserInfoController {
    @Autowired
    private UserService userService;
    @GetMapping(value = "/users",name = "查询用户信息")
    public ResponseEntity findUSerInfoVo(Long userID, Long huanxinID){

        //如果有先根据userID和huanxinID查询（现在不做，后面做）
        if (userID!=null) {
            return userService.findUserInfoVo(userID);
        }else if (huanxinID!=null){
            return userService.findUserInfoVo(huanxinID);
        }else {
            User user = UserHolder.getUser();
            ResponseEntity userInfoVo = userService.findUserInfoVo(user.getId());
            System.out.println(userInfoVo);
            return userInfoVo;
        }
    }
    @PostMapping(value = "/users/header",name = "查询用户信息")
    public ResponseEntity updatePic(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
       userService.loginReginfoPic(headPhoto,token);
       return ResponseEntity.ok(null);
    }
    @PutMapping(value = "/users",name = "查询用户信息")
    public ResponseEntity updateUSerInfo(@RequestBody UserInfo userInfo, Long userID, Long huanxinID, @RequestHeader("Authorization") String token){
        userService.updateUserInfo(userInfo,token);
        return ResponseEntity.ok(null);
    }
    @GetMapping(value = "/users/counts",name = "统计好友 喜欢 被喜欢数量")
    public ResponseEntity counts() throws ClientException, IOException {
        return userService.counts();
    }

    @GetMapping(value="/users/friends/{type}",name="互相喜欢，喜欢，粉丝列表查询")
    public ResponseEntity findMyCountList(@PathVariable("type") Integer type,Integer page,Integer pagesize){
        return userService.findMyCountList(type,page,pagesize);
    }
    @PostMapping(value = "/users/fans/{uid}",name="粉丝变好友")
    public ResponseEntity fanstoFriend(@PathVariable("uid") Long fanId) throws IOException {
        return userService.fansToFriend(fanId);
    }

    @DeleteMapping(value = "/users/like/{uid}",name="取消喜欢(互相喜欢的取消或者拒接当舔狗)")
    public ResponseEntity removeLove(@PathVariable("uid") Long loveId) throws IOException {
        return userService.removeLove(loveId);
    }

}
